package me.chan.helper;

import me.chan.annotation.Controller;
import me.chan.annotation.Service;
import me.chan.util.ClassUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by JanGin on 2017/6/16.
 */
public class ClassHelper {

    //a container to store all the class entity
    private static final Set<Class<?>> CLASS_SET;

    static {
        String APP_BASE_PACKAGE = ConfigHelper.getAppBasePackage();
        CLASS_SET = ClassUtil.getClassSet(APP_BASE_PACKAGE);
    }

    public static Set<Class<?>> getClassSet() {
        return CLASS_SET;
    }

    public static Set<Class<?>> getServiceClassSet() {
        Set<Class<?>> set = new HashSet<>();
        for (Class clazz : CLASS_SET) {
            if (clazz.isAnnotationPresent(Service.class)) {
                set.add(clazz);
            }
        }
        return set;
    }

    public static Set<Class<?>> getControllerClassSet() {
        Set<Class<?>> set = new HashSet<>();
        for (Class clazz : CLASS_SET) {
            if (clazz.isAnnotationPresent(Controller.class)) {
                set.add(clazz);
            }
        }
        return set;
    }

    public static Set<Class<?>> getBeanClassSet() {
        Set<Class<?>> set = new HashSet<>();
        set.addAll(getServiceClassSet());
        set.addAll(getControllerClassSet());
        return set;
    }
}
